package com.come4loves.game.module.fight.model;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DeskOfCard<T> {
	private final Logger LOGGER = LoggerFactory.getLogger(DeskOfCard.class);
	/**
	 * 原始卡组
	 */
	private List<T> cards;
	private ArrayDeque<T> queue = new ArrayDeque<T>();

	public DeskOfCard(List<T> cards) {
		this.cards = cards;
	}
	
	public List<T> getCards() {
		return cards;
	}

	/**
	 * 添加卡牌
	 * @param addCards
	 * @param top 是否在牌顶
	 */
	public void add(List<T> addCards, boolean top) {
		for (T t : addCards) {
			if (t != null) {
				if (top) {
					queue.addFirst(t);
				} else {
					queue.addLast(t);
				}
			} else {
				LOGGER.warn("add has null card");
			}
		}
	}

	/**
	 * 洗牌
	 */
	public void shuffleCard() {
		List<T> queueCard = new ArrayList<T>();
		queueCard.addAll(this.cards);
		Collections.shuffle(queueCard);
		queue.clear();
		for (int i = 0; i < queueCard.size(); i++) {
			queue.add(queueCard.get(i));
		}
	}
	public void shuffleCard(List<T> cards) {
		Collections.shuffle(cards);
		for (int i = 0; i < cards.size(); i++) {
			queue.add(cards.get(i));
		}
	}

	/**
	 * 重置并洗牌
	 */
	public void resetAndShuffleCard() {
		queue.clear();
		shuffleCard();
	}

	public List<T> dealCard(int num) {
		int dealNum = num > queue.size()? queue.size() : num;
		List<T> dealCards = new ArrayList<T>();
		for (int i = 0; i < dealNum; i++) {
			T temp = queue.poll();
			dealCards.add(temp);
		}
		return dealCards;
	}

	public int remaining() {
		return queue.size();
	}
	
	public List<T> remainCards(){
		List<T> list = new ArrayList<T>();
		Iterator<T> it = this.queue.iterator();
		while(it.hasNext()) {
			list.add(it.next());
		}
		return list;
	}

}
